package commonInfrastructure.communication;

import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.LocalBean;
 
@Singleton
@Startup
@LocalBean
public class MessageManager{
 
    private final List messages = Collections.synchronizedList(new LinkedList());
 
    public void sendMessage(Message msg) {
        messages.add(msg);
        msg.setDateSent(new Date());
    }
 
    public Message getFirstAfter(Date after, String user) {
        if(messages.isEmpty())
            return null;
        if(after == null)
            return (Message)messages.get(0);
        for(Object o : messages) {
            Message m = (Message) o;
            if(m.getDateSent().after(after) && (m.getToUser().contains(user)||m.getUser().equals(user)))
                return m;
        }
        return null;
    }
}